home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Runtime.java < prev    next >
Text File  |  1998-09-22  |  19KB  |  491 lines

  1. /*
  2.  * @(#)Runtime.java    1.27 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. import java.io.*;
  18. import java.util.StringTokenizer;
  19.  
  20. /*
  21.  * Every Java application has a single instance of class 
  22.  * <code>Runtime</code> that allows the application to interface with 
  23.  * the environment in which the application is running. The current 
  24.  * runtime can be obtained from the <code>getRuntime</code> method. 
  25.  * <p>
  26.  * An application cannot create its own instance of this class. 
  27.  *
  28.  * @author  unascribed
  29.  * @version 1.27, 07/01/98
  30.  * @see     java.lang.Runtime#getRuntime()
  31.  * @since   JDK1.0
  32.  */
  33. public class Runtime {
  34.     private static Runtime currentRuntime = new Runtime();
  35.       
  36.     /**
  37.      * Returns the runtime object associated with the current Java application.
  38.      *
  39.      * @return  the <code>Runtime</code> object associated with the current
  40.      *          Java application.
  41.      * @since   JDK1.0
  42.      */
  43.     public static Runtime getRuntime() { 
  44.     return currentRuntime;
  45.     }
  46.     
  47.     /** Don't let anyone else instantiate this class */
  48.     private Runtime() {}
  49.  
  50.     /* Helper for exit
  51.      */
  52. //__SYMC__
  53.     private native synchronized void exitInternal(int status);
  54. //  private native void exitInternal(int status);
  55. //__SYMC__
  56.  
  57.     /**
  58.      * Terminates the currently running Java Virtual Machine. This 
  59.      * method never returns normally. 
  60.      * <p>
  61.      * If there is a security manager, its <code>checkExit</code> method 
  62.      * is called with the status as its argument. This may result in a 
  63.      * security exception. 
  64.      * <p>
  65.      * The argument serves as a status code; by convention, a nonzero 
  66.      * status code indicates abnormal termination. 
  67.      *
  68.      * @param      status   exit status.
  69.      * @exception  SecurityException  if the current thread cannot exit with
  70.      *               the specified status.
  71.      * @see        java.lang.SecurityException
  72.      * @see        java.lang.SecurityManager#checkExit(int)
  73.      * @since      JDK1.0
  74.      */
  75.     public void exit(int status) {
  76.     SecurityManager security = System.getSecurityManager();
  77.     if (security != null) {
  78.         security.checkExit(status);
  79.     }
  80.     exitInternal(status);
  81.     }
  82.  
  83.     /**
  84.      * Enable or disable finalization on exit; doing so specifies that the
  85.      * finalizers of all objects that have finalizers that have not yet been
  86.      * automatically invoked are to be run before the Java runtime exits.
  87.      * By default, finalization on exit is disabled.  An invocation of
  88.      * the runFinalizersOnExit method is permitted only if the caller is
  89.      * allowed to exit, and is otherwise rejected by the security manager.
  90.      * @see Runtime#gc
  91.      * @see Runtime#exit
  92.      * @since   JDK1.1
  93.      */
  94.     public static void runFinalizersOnExit(boolean value) {
  95.     SecurityManager security = System.getSecurityManager();
  96.     if (security != null) {
  97.         try { security.checkExit(0); }
  98.         catch (SecurityException e) {
  99.         throw new SecurityException("runFinalizersOnExit");
  100.         }
  101.     }
  102.     runFinalizersOnExit0(value);
  103.     }
  104.  
  105.     /*
  106.      * Private variable holding the boolean determining whether to finalize
  107.      * on exit.  The default value of the variable is false.  See the comment
  108.      * on Runtime.runFinalizersOnExit for constraints on modifying this.
  109.      */
  110.     private static native void runFinalizersOnExit0(boolean value);
  111.  
  112.     /* Helper for exec
  113.      */
  114.     private native Process execInternal(String cmdarray[], String envp[]) 
  115.      throws IOException;
  116.  
  117.     /**
  118.      * Executes the specified string command in a separate process. 
  119.      * <p>
  120.      * The <code>command</code> argument is parsed into tokens and then 
  121.      * executed as a command in a separate process. This method has 
  122.      * exactly the same effect as <code>exec(command, null)</code>. 
  123.      *
  124.      * @param      command   a specified system command.
  125.      * @return     a <code>Process</code> object for managing the subprocess.
  126.      * @exception  SecurityException  if the current thread cannot create a
  127.      *             subprocess.
  128.      * @see        java.lang.Runtime#exec(java.lang.String, java.lang.String[])
  129.      * @since      JDK1.0
  130.      */
  131.     public Process exec(String command) throws IOException {
  132.     return exec(command, null);
  133.     }
  134.  
  135.     /**
  136.      * Executes the specified string command in a separate process with the 
  137.      * specified environment. 
  138.      * <p>
  139.      * This method breaks the <code>command</code> string into tokens and 
  140.      * creates a new array <code>cmdarray</code> containing the tokens; it 
  141.      * then performs the call <code>exec(cmdarray, envp)</code>. 
  142.      *
  143.      * @param      command   a specified system command.
  144.      * @param      envp      array containing environment in format
  145.      *                       <i>name</i>=<i>value</i>
  146.      * @return     a <code>Process</code> object for managing the subprocess.
  147.      * @exception  SecurityException  if the current thread cannot create a
  148.      *               subprocess.
  149.      * @see        java.lang.Runtime#exec(java.lang.String[], java.lang.String[])
  150.      * @since      JDK1.0
  151.      */
  152.     public Process exec(String command, String envp[]) throws IOException {
  153.     int count = 0;
  154.     String cmdarray[];
  155.      StringTokenizer st;
  156.  
  157.     st = new StringTokenizer(command);
  158.      count = st.countTokens();
  159.  
  160.     cmdarray = new String[count];
  161.     st = new StringTokenizer(command);
  162.     count = 0;
  163.      while (st.hasMoreTokens()) {
  164.          cmdarray[count++] = st.nextToken();
  165.      }
  166.     SecurityManager security = System.getSecurityManager();
  167.     if (security != null) {
  168.         security.checkExec(cmdarray[0]);
  169.     }
  170.     return execInternal(cmdarray, envp);
  171.     }
  172.  
  173.     /**
  174.      * Executes the specified command and arguments in a separate process.
  175.      * <p>
  176.      * The command specified by the tokens in <code>cmdarray</code> is 
  177.      * executed as a command in a separate process. This has exactly the 
  178.      * same effect as <code>exec(cmdarray, null)</code>. 
  179.      *
  180.      * @param      cmdarray   array containing the command to call and
  181.      *                        its arguments.
  182.      * @return     a <code>Process</code> object for managing the subprocess.
  183.      * @exception  SecurityException  if the current thread cannot create a
  184.      *               subprocess.
  185.      * @see        java.lang.Runtime#exec(java.lang.String[], java.lang.String[])
  186.      * @since      JDK1.0
  187.      */
  188.     public Process exec(String cmdarray[]) throws IOException {
  189.     return exec(cmdarray, null);
  190.     }
  191.  
  192.     /**
  193.      * Executes the specified command and arguments in a separate process
  194.      * with the specified environment. 
  195.      * <p>
  196.      * If there is a security manager, its <code>checkExec</code> method 
  197.      * is called with the first component of the array 
  198.      * <code>cmdarray</code> as its argument. This may result in a security 
  199.      * exception. 
  200.      * <p>
  201.      * Given an array of strings <code>cmdarray</code>, representing the 
  202.      * tokens of a command line, and an array of strings <code>envp</code>, 
  203.      * representing an "environment" that defines system 
  204.      * properties, this method creates a new process in which to execute 
  205.      * the specified command. 
  206.      *
  207.      * @param      cmdarray   array containing the command to call and
  208.      *                        its arguments.
  209.      * @param      envp       array containing environment in format
  210.      *                        <i>name</i>=<i>value</i>.
  211.      * @return     a <code>Process</code> object for managing the subprocess.
  212.      * @exception  SecurityException  if the current thread cannot create a
  213.      *               subprocess.
  214.      * @see     java.lang.SecurityException
  215.      * @see     java.lang.SecurityManager#checkExec(java.lang.String)
  216.      * @since   JDK1.0
  217.      */
  218.     public Process exec(String cmdarray[], String envp[]) throws IOException {
  219.     SecurityManager security = System.getSecurityManager();
  220.     if (security != null) {
  221.         security.checkExec(cmdarray[0]);
  222.     }
  223.     return execInternal(cmdarray, envp);
  224.     }
  225.  
  226.     /**
  227.      * Returns the amount of free memory in the system. The value returned
  228.      * by this method is always less than the value returned by the
  229.      * <code>totalMemory</code> method. Calling the <code>gc</code> method may
  230.      * result in increasing the value returned by <code>freeMemory.</code>
  231.      *
  232.      * @return  an approximation to the total amount of memory currently
  233.      *          available for future allocated objects, measured in bytes.
  234.      * @since   JDK1.0
  235.      */
  236.     public native long freeMemory();
  237.  
  238.     /**
  239.      * Returns the total amount of memory in the Java Virtual Machine. 
  240.      *
  241.      * @return  the total amount of memory currently available for allocating
  242.      *          objects, measured in bytes.
  243.      * @since   JDK1.0
  244.      */
  245.     public native long totalMemory();
  246.  
  247.     /**
  248.      * Runs the garbage collector.
  249.      * Calling this method suggests that the Java Virtual Machine expend 
  250.      * effort toward recycling unused objects in order to make the memory 
  251.      * they currently occupy available for quick reuse. When control 
  252.      * returns from the method call, the Java Virtual Machine has made 
  253.      * its best effort to recycle all unused objects. 
  254.      * <p>
  255.      * The name <code>gc</code> stands for "garbage 
  256.      * collector". The Java Virtual Machine performs this recycling 
  257.      * process automatically as needed even if the <code>gc</code> method 
  258.      * is not invoked explicitly. 
  259.      *
  260.      * @since   JDK1.0
  261.      */
  262.     public native void gc();
  263.  
  264.     /**
  265.      * Runs the finalization methods of any objects pending finalization.
  266.      * Calling this method suggests that the Java Virtual Machine expend 
  267.      * effort toward running the <code>finalize</code> methods of objects 
  268.      * that have been found to be discarded but whose <code>finalize</code> 
  269.      * methods have not yet been run. When control returns from the 
  270.      * method call, the Java Virtual Machine has made a best effort to 
  271.      * complete all outstanding finalizations. 
  272.      * <p>
  273.      * The Java Virtual Machine performs the finalization process 
  274.      * automatically as needed if the <code>runFinalization</code> method 
  275.      * is not invoked explicitly. 
  276.      *
  277.      * @see     java.lang.Object#finalize()
  278.      * @since   JDK1.0
  279.      */
  280.     public native void runFinalization();
  281.  
  282.     /**
  283.      * Enables/Disables tracing of instructions.
  284.      * If the <code>boolean</code> argument is <code>true</code>, this 
  285.      * method asks the Java Virtual Machine to print out a detailed trace 
  286.      * of each instruction in the Java Virtual Machine as it is executed. 
  287.      * The virtual machine may ignore this request if it does not support 
  288.      * this feature. The destination of the trace output is system 
  289.      * dependent. 
  290.      * <p>
  291.      * If the <code>boolean</code> argument is <code>false</code>, this 
  292.      * method causes the Java Virtual Machine to stop performing the 
  293.      * detailed instruction trace it is performing. 
  294.      *
  295.      * @param   on   <code>true</code> to enable instruction tracing;
  296.      *               <code>false</code> to disable this feature.
  297.      * @since   JDK1.0
  298.      */
  299.     public native void traceInstructions(boolean on);
  300.  
  301.     /**
  302.      * Enables/Disables tracing of method calls.
  303.      * If the <code>boolean</code> argument is <code>true</code>, this 
  304.      * method asks the Java Virtual Machine to print out a detailed trace 
  305.      * of each method in the Java Virtual Machine as it is called. The 
  306.      * virtual machine may ignore this request if it does not support 
  307.      * this feature. The destination of the trace output is system dependent. 
  308.      * <p>
  309.      * If the <code>boolean</code> argument is <code>false</code>, this 
  310.      * method causes the Java Virtual Machine to stop performing the 
  311.      * detailed method trace it is performing. 
  312.      *
  313.      * @param   on   <code>true</code> to enable instruction tracing;
  314.      *               <code>false</code> to disable this feature.
  315.      * @since   JDK1.0
  316.      */
  317.     public native void traceMethodCalls(boolean on);
  318.  
  319.     /**
  320.      * Initializes the linker and returns the search path for shared libraries.
  321.      */
  322.     private synchronized native String initializeLinkerInternal();
  323.     private native String buildLibName(String pathname, String filename);
  324.  
  325.     /* Helper for load and loadLibrary */
  326.     private native int loadFileInternal(String filename);
  327.  
  328.     /** The paths searched for libraries */
  329.     private String paths[];
  330.  
  331.     private void initializeLinker() {
  332.     String ldpath = initializeLinkerInternal();
  333.     char c = System.getProperty("path.separator").charAt(0);
  334.     int ldlen = ldpath.length();
  335.     int i, j, n;
  336.     // Count the separators in the path
  337.     i = ldpath.indexOf(c);
  338.     n = 0;
  339.     while (i >= 0) {
  340.         n++;
  341.         i = ldpath.indexOf(c, i+1);
  342.     }
  343.  
  344.     // allocate the array of paths - n :'s = n + 1 path elements
  345.     paths = new String[n + 1];
  346.  
  347.     // Fill the array with paths from the ldpath
  348.     n = i = 0;
  349.     j = ldpath.indexOf(c);
  350.     while (j >= 0) {
  351.         if (j - i > 0) {
  352.         paths[n++] = ldpath.substring(i, j);
  353.         } else if (j - i == 0) { 
  354.         paths[n++] = ".";
  355.         }
  356.         i = j + 1;
  357.         j = ldpath.indexOf(c, i);
  358.     }
  359.     paths[n] = ldpath.substring(i, ldlen);
  360.     }
  361.  
  362.     /**
  363.      * Loads the specified filename as a dynamic library. The filename 
  364.      * argument must be a complete pathname. 
  365.      * From <code>java_g</code> it will automagically insert "_g" before the
  366.      * ".so" (for example
  367.      * <code>Runtime.getRuntime().load("/home/avh/lib/libX11.so");</code>).
  368.      * <p>
  369.      * If there is a security manager, its <code>checkLink</code> method 
  370.      * is called with the <code>filename</code> as its argument. This may 
  371.      * result in a security exception. 
  372.      *
  373.      * @param      filename   the file to load.
  374.      * @exception  SecurityException     if the current thread cannot load the
  375.      *               specified dynamic library.
  376.      * @exception  UnsatisfiedLinkError  if the file does not exist.
  377.      * @see        java.lang.Runtime#getRuntime()
  378.      * @see        java.lang.SecurityException
  379.      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
  380.      * @since      JDK1.0
  381.      */
  382.     public synchronized void load(String filename) {
  383.     SecurityManager security = System.getSecurityManager();
  384.     int ret;
  385.     if (security != null) {
  386.         security.checkLink(filename);
  387.     }
  388.     ret = loadFileInternal(filename);
  389.     if (ret == -1) {
  390.         throw new OutOfMemoryError();
  391.     } else if (ret == 0) {
  392.         throw new UnsatisfiedLinkError(filename);
  393.     }   /* else load was successful; return */
  394.     }
  395.  
  396.     /**
  397.      * Loads the dynamic library with the specified library name. The 
  398.      * mapping from a library name to a specific filename is done in a 
  399.      * system-specific manner. 
  400.      * <p>
  401.      * First, if there is a security manager, its <code>checkLink</code> 
  402.      * method is called with the <code>filename</code> as its argument. 
  403.      * This may result in a security exception. 
  404.      * <p>
  405.      * If this method is called more than once with the same library 
  406.      * name, the second and subsequent calls are ignored. 
  407.      *
  408.      * @param      libname   the name of the library.
  409.      * @exception  SecurityException     if the current thread cannot load the
  410.      *               specified dynamic library.
  411.      * @exception  UnsatisfiedLinkError  if the library does not exist.
  412.      * @see        java.lang.SecurityException
  413.      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
  414.      * @since      JDK1.0
  415.      */
  416.     public synchronized void loadLibrary(String libname) {
  417.     SecurityManager security = System.getSecurityManager();
  418.     if (security != null) {
  419.         security.checkLink(libname);
  420.     }
  421.         if (paths == null) {
  422.             initializeLinker();
  423.     }
  424.     for (int i = 0 ; i < paths.length ; i++) {
  425.         int ret;
  426.         String tempname = buildLibName(paths[i], libname);
  427.         ret = loadFileInternal(tempname);
  428.         if (ret == -1) {
  429.         throw new OutOfMemoryError();
  430.         } else if (ret == 1) {    // Loaded or found it already loaded
  431.         return;
  432.         }
  433.     }
  434.     // Oops, it failed
  435.         throw new UnsatisfiedLinkError("no " + libname + 
  436.                        " in shared library path");
  437.     }
  438.  
  439.     /**
  440.      * Creates a localized version of an input stream. This method takes 
  441.      * an <code>InputStream</code> and returns an <code>InputStream</code> 
  442.      * equivalent to the argument in all respects except that it is 
  443.      * localized: as characters in the local character set are read from 
  444.      * the stream, they are automatically converted from the local 
  445.      * character set to Unicode. 
  446.      * <p>
  447.      * If the argument is already a localized stream, it may be returned 
  448.      * as the result. 
  449.      *
  450.      * @deprecated As of JDK 1.1, the preferred way translate a byte
  451.      * stream in the local encoding into a character stream in Unicode is via
  452.      * the <code>InputStreamReader</code> and <code>BufferedReader</code>
  453.      * classes.
  454.      *
  455.      * @return     a localized input stream.
  456.      * @see        java.io.InputStream
  457.      * @see        java.io.BufferedReader#BufferedReader(java.io.Reader)
  458.      * @see        java.io.InputStreamReader#InputStreamReader(java.io.InputStream)
  459.      */
  460.     public InputStream getLocalizedInputStream(InputStream in) {
  461.     return in;
  462.     }
  463.  
  464.     /**
  465.      * Creates a localized version of an output stream. This method 
  466.      * takes an <code>OutputStream</code> and returns an 
  467.      * <code>OutputStream</code> equivalent to the argument in all respects 
  468.      * except that it is localized: as Unicode characters are written to 
  469.      * the stream, they are automatically converted to the local 
  470.      * character set. 
  471.      * <p>
  472.      * If the argument is already a localized stream, it may be returned 
  473.      * as the result. 
  474.      *
  475.      * @deprecated As of JDK 1.1, the preferred way to translate a
  476.      * Unicode character stream into a byte stream in the local encoding is via
  477.      * the <code>OutputStreamWriter</code>, <code>BufferedWriter</code>, and
  478.      * <code>PrintWriter</code> classes.
  479.      *
  480.      * @return     a localized output stream.
  481.      * @see        java.io.OutputStream
  482.      * @see        java.io.BufferedWriter#BufferedWriter(java.io.Writer)
  483.      * @see        java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
  484.      * @see        java.io.PrintWriter#PrintWriter(java.io.OutputStream)
  485.      */
  486.     public OutputStream getLocalizedOutputStream(OutputStream out) {
  487.     return out;
  488.     }
  489.  
  490. }
  491.